package com.example.mobile1931605084chy0604;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

import javax.xml.validation.Validator;

public class MainActivity extends AppCompatActivity {

    private Button btnMainStars;
    private Button btnMainStops;
    MusicService musicService;
    ServiceConnection serviceConnection;
    private Button button4;
    private Button btnMainPauseMusic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        final Intent intent = new Intent(MainActivity.this, MusicService.class);
        serviceConnection = new ServiceConnection() {
            //服务链接
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                musicService = ((MusicService.ServiceBinder) service).getMusicService();
            }

            //服务断开
            @Override
            public void onServiceDisconnected(ComponentName name) {
                musicService = null;
            }
        };
        btnMainStars.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                startService(intent);
                bindService(intent, serviceConnection, BIND_AUTO_CREATE);
            }
        });
        btnMainStops.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                stopService(intent);
                musicService.playMusic();
            }
        });
        btnMainPauseMusic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                musicService.pauseMusic();
            }
        });
        button4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                unbindService(serviceConnection);
                musicService.replayMusic();
            }
        });
    }

    private void initView() {
        btnMainStars = (Button) findViewById(R.id.btnMainStars);
        btnMainStops = (Button) findViewById(R.id.btnMainStops);
        button4 = (Button) findViewById(R.id.button4);
        btnMainPauseMusic = (Button) findViewById(R.id.btnMainPauseMusic);
    }
}